import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class demo15 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
        //生产者 消费者模型

        Thread producer = new Thread(()->{
           int n = 0;
           while(true) {
               try {
                   queue.put(n);
                   System.out.println("加入元素: " + n);
                   n++;
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        },"producer");

        Thread consumer = new Thread(()->{
            while(true) {
                try {
                    int n = queue.take();
                    System.out.println("处理元素: " + n);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"consumer");
        producer.start();
        consumer.start();
    }
}
